package org.southyin.controller.cms_customer;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.southyin.commons.CommonsResp;
import org.southyin.model.CmsCustomer;
import org.southyin.service.CmsCustomerService;
import org.southyin.service.impl.CmsCustomerServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "CustomerRegServlet",value = "/user/reg")
public class CustomerRegServlet extends HttpServlet {
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String email = req.getParameter("email");
        String password = req.getParameter("password");
        PrintWriter writer = resp.getWriter();
        if (email == null || email.trim().equals("") || password == null || password.trim().equals("")) {
            CommonsResp commonsResp = CommonsResp.failed(400,"邮箱或密码不能为空");
            writer.println(new ObjectMapper().writeValueAsString(commonsResp));
        }
        else{
            // 封装数据
            CmsCustomer customer = new CmsCustomer();
            customer.setEmail(email);
            customer.setPassword(password);
            // 调 service 注册业务
            CmsCustomerService service = new CmsCustomerServiceImpl();
            CommonsResp commonsResp = service.customerReg(customer);
            writer.println(new ObjectMapper().writeValueAsString(commonsResp));
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
